package sculove.spring.framework.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;

/**
 * @author sculove
 * @deprecated
 */
public class FileUtil {
    private BufferedReader br = null;
    private PrintWriter pw = null;
    private File inputFile = null ;
    private File outputFile = null ;
    private static final int MAXCOUNT = 1024*1024*50;     // Buffer Size

    /**
     * 생성자
     * @throws Exception
     */
    public FileUtil() throws Exception
    {
    	// default constructor
    }

    /**
     * 입력용 파일을 얻음
     * @return FILE
     * @throws Exception
     */
    public File getInputFile() throws Exception
    {
    	 return  this.inputFile ;
    }

    /**
     * 출력용 파일을 얻음
     * @return FILE
     * @throws Exception
     */
    public File getOutputFile() throws Exception
    {
    	 return  this.outputFile ;
    }

    /**
     * 입력 파일 존재여부확인.
     * @param tempFile
     * @return
     * @throws Exception
     */
    private boolean isInputFileExist(File tempFile) throws Exception
    {
    	if (tempFile.exists()) {
           br = new BufferedReader( new FileReader( inputFile ) );
           return true ;
    	}  else return false ;
    }

    /**
     * 입력용 파일을 설정함.
     * @param inputFile
     * @return
     * @throws Exception
     */
    public boolean setInputFile(File inputFile) throws Exception
    {
    	this.inputFile = inputFile;
        return isInputFileExist(this.inputFile);
    }

    /**
     * 입력용 파일을 설정함.
     * @param fileName
     * @return
     * @throws Exception
     */
    public boolean setInputFile(String fileName) throws Exception
    {
    	this.inputFile = new File(fileName) ;
        return isInputFileExist(this.inputFile);
    }

    /**
     * 입력용 파일을 설정함.
     * @param filePath
     * @param fileName
     * @return
     * @throws Exception
     */
    public boolean setInputFile(String filePath, String fileName) throws Exception
    {
    	this.inputFile = new File(filePath , fileName) ;
        return isInputFileExist(this.inputFile );
    }

    /**
     * 출력용 파일을 설정함
     * @param outputFile
     * @throws Exception
     */
    public void setOutputFile(File outputFile) throws Exception
    {
    	this.outputFile = outputFile;
    	new File(this.outputFile.getParent()).mkdirs();
        pw = new PrintWriter( new FileWriter( this.outputFile ) );
    }

    /**
     * 출력용 파일을 설정함.
     * @param fileName
     * @throws Exception
     */
    public void setOutputFile(String fileName) throws Exception
    {
         this.outputFile = new File(  fileName ) ;
//    	 new File(this.outputFile.getParent()).mkdirs();
         pw = new PrintWriter( new FileWriter( this.outputFile ) );
    }

    /**
     * 출력용 파일을 설정함.
     * @param filePath
     * @param fileName
     * @throws Exception
     */
    public void setOutputFile(String filePath, String fileName) throws Exception
    {

    	  this.outputFile = new File( filePath , fileName ) ;
    	  new File(this.outputFile.getParent()).mkdirs();
    	  pw = new PrintWriter( new FileWriter( this.outputFile ) );
    }

    /**
     * 파일 close
     * @throws Exception
     */
    public void closeFiles() throws Exception
    {
	     if( br != null ) {
	    	 br.close();
	    	 br = null;
	     }
         if( pw != null ) {
        	 pw.close();
        	 pw = null;
         }
    }

    /**
     * 출력파일에 쓰기
     * @param oneLine
     * @throws Exception
     */
    public void println(String oneLine) throws Exception
    {
    	pw.println( oneLine ) ;
    }

    /**
     * 출력파일에 쓰기
     * @param part
     * @throws Exception
     */
    public void print( String part ) throws Exception
    {
    	pw.print( part ) ;
    }

    /**
     * 출력파일의 PrintWriter객체 얻기
     * @return
     * @throws Exception
     */
    public PrintWriter getPrintWriter() throws Exception
    {
    	return this.pw ;
    }

    /**
     * 입력파일 읽기
     * @return
     * @throws Exception
     */
    public String readLine() throws Exception
    {
    	return br.readLine() ;
    }

    /**
     * 입력파일 읽기
     * @param cbuf
     * @param off
     * @param len
     * @return
     * @throws Exception
     */
    public int read(char[] cbuf,int off, int len) throws Exception
    {
        return br.read( cbuf , off , len ) ;
    }

    /**
     * 입력파일 BufferedReader 얻기
     * @return
     * @throws Exception
     */
    public BufferedReader getBufferedReader() throws Exception
    {
    	return this.br;
    }



    /**
     * 파일 삭제
     * @param fileName
     * @return
     */
    public static boolean remove(String fileName)
    {
        File f = new File(fileName);
        return f.delete();
    }


    /**
     * 파일 복사하는 모듈 NIO를 이용하여 복사.
     * @param inFileName
     * @param outFileName
     * @return
     */
    public static boolean copy(String srcFileName, String destFileName) {
    	FileChannel in  = null;
        FileChannel out = null;

        try {
        	// 상위 디렉토리 자동생성.
            File destFile = new File(destFileName) ;
            new File(destFile.getParent()).mkdirs();
        	in  = new FileInputStream(srcFileName).getChannel();
            out = new FileOutputStream(destFileName).getChannel();

            long size = in.size();
            long position = 0;
//                System.out.println("size:"+size);
            while (position < size) {
//            	System.out.println("position:"+position);
               position += in.transferTo(position, FileUtil.MAXCOUNT, out);
            }
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
	        try { if(in != null) { in.close(); in = null; } } catch (Exception e) {}
	        try { if(out != null) { out.close(); out = null; } } catch (Exception e) {}
		}
		return true;
    }
}
